
package org.apache.solr.schema;

import java.util.HashMap;
import java.util.Map;

/**
 * @lucene.internal
 */
public abstract class FieldProperties {

    // use a bitfield instead of many different boolean variables since
    // many of the variables are independent or semi-independent.
    // bit values for boolean field properties.
    protected final static int INDEXED              = 0x0000_0001;
    protected final static int TOKENIZED            = 0x0000_0002;
    protected final static int STORED               = 0x0000_0004;
    protected final static int BINARY               = 0x0000_0008;
    protected final static int OMIT_NORMS           = 0x0000_0010;
    protected final static int OMIT_TF_POSITIONS    = 0x0000_0020;
    protected final static int STORE_TERMVECTORS    = 0x0000_0040;
    protected final static int STORE_TERMPOSITIONS  = 0x0000_0080;
    protected final static int STORE_TERMOFFSETS    = 0x0000_0100;
    protected final static int MULTIVALUED          = 0x0000_0200;
    protected final static int SORT_MISSING_FIRST   = 0x0000_0400;
    protected final static int SORT_MISSING_LAST    = 0x0000_0800;
    protected final static int REQUIRED             = 0x0000_1000;
    protected final static int OMIT_POSITIONS       = 0x0000_2000;
    static final String[] propertyNames = {
        "indexed", "tokenized", "stored",
        "binary", "omitNorms", "omitTermFreqAndPositions",
        "termVectors", "termPositions", "termOffsets",
        "multiValued",
        "sortMissingFirst", "sortMissingLast", "required", "omitPositions"
    };
    static final Map<String, Integer> propertyMap = new HashMap<>();

    static {
        for (String prop : propertyNames) {
            propertyMap.put(prop, propertyNameToInt(prop));
        }
    }

    /**
     * Returns the symbolic name for the property.
     */
    static String getPropertyName(int property) {
        return propertyNames[ Integer.numberOfTrailingZeros(property)];
    }

    static int propertyNameToInt(String name) {

        for (int i = 0; i < propertyNames.length; i++) {
            if (propertyNames[i].equals(name)) {
                return 1 << i;
            }
        }
        return 0;
    }

    static String propertiesToString(int properties) {

        StringBuilder sb = new StringBuilder();
        boolean first = true;
        while (properties != 0) {
            if (!first) {
                sb.append(',');
            }
            first = false;
            int bitpos = Integer.numberOfTrailingZeros(properties);
            sb.append(getPropertyName(1 << bitpos));
            properties &= ~(1 << bitpos);  // clear that bit position
        }
        return sb.toString();
    }

    static boolean on(int bitfield, int props) {
        return (bitfield & props) != 0;
    }

    static boolean off(int bitfield, int props) {
        return (bitfield & props) == 0;
    }

    static int parseProperties(Map<String, String> properties, boolean which) {

        int props = 0;
        for (Map.Entry<String, String> entry : properties.entrySet()) {
            String val = entry.getValue();
            if (val == null) {
                continue;
            }
            if (Boolean.parseBoolean(val) == which) {
                props |= propertyNameToInt(entry.getKey());
            }
        }
        return props;
    }
}
